package com.ibigcat.module.mdm.service.impl;

import com.ibigcat.module.mdm.service.OrgService;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.ibigcat.module.mdm.controller.admin.org.vo.*;
import com.ibigcat.module.mdm.dao.entity.OrgDO;
import com.ibigcat.framework.common.pojo.PageResult;
import com.ibigcat.framework.common.pojo.PageParam;
import com.ibigcat.framework.common.util.object.BeanUtils;

import com.ibigcat.module.mdm.dao.mapper.OrgMapper;

import static com.ibigcat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.ibigcat.module.mdm.enums.ErrorCodeConstants.*;

/**
 * 组织机构 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class OrgServiceImpl implements OrgService {

    @Resource
    private OrgMapper orgMapper;

    @Override
    public Long createOrg(OrgSaveReqVO createReqVO) {
        // 插入
        OrgDO org = BeanUtils.toBean(createReqVO, OrgDO.class);
        orgMapper.insert(org);
        // 返回
        return org.getId();
    }

    @Override
    public void updateOrg(OrgSaveReqVO updateReqVO) {
        // 校验存在
        validateOrgExists(updateReqVO.getId());
        // 更新
        OrgDO updateObj = BeanUtils.toBean(updateReqVO, OrgDO.class);
        orgMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrg(Long id) {
        // 校验存在
        validateOrgExists(id);
        // 删除
        orgMapper.deleteById(id);
    }

    private void validateOrgExists(Long id) {
        if (orgMapper.selectById(id) == null) {
            throw exception(ORG_NOT_EXISTS);
        }
    }

    @Override
    public OrgDO getOrg(Long id) {
        return orgMapper.selectById(id);
    }

    @Override
    public PageResult<OrgDO> getOrgPage(OrgPageReqVO pageReqVO) {
        return orgMapper.selectPage(pageReqVO);
    }

}